package cn.wetcland.es.service.impl;

import cn.wetcland.es.dto.ClassUpdateDTO;
import cn.wetcland.es.dto.PageDTO;
import cn.wetcland.es.utils.MyAssert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wetcland.es.domain.EsClass;
import cn.wetcland.es.service.EsClassService;
import cn.wetcland.es.mapper.EsClassMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author Wetcland
* @description 针对表【es_class(班级表)】的数据库操作Service实现
* @createDate 2023-04-29 17:04:49
*/
@Service
public class EsClassServiceImpl extends ServiceImpl<EsClassMapper, EsClass> implements EsClassService{

    @Resource
    private EsClassMapper classMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNewClass(String className) {
        EsClass esClass = classMapper.selectOne(new QueryWrapper<EsClass>().eq("es_class_name", className));
        MyAssert.isNull(esClass, "已存在该班级");
        EsClass ec = new EsClass();
        ec.setEsClassName(className);
        classMapper.insert(ec);
    }

    @Override
    public Page<EsClass> getPageClassList(PageDTO pageDTO) {
        Page<EsClass> esClassPage = new Page<>(pageDTO.getCurPage(), pageDTO.getPageSize());
        return classMapper.selectPage(esClassPage, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteClass(Long classId) {
        EsClass esClass = classMapper.selectById(classId);
        MyAssert.notNull(esClass, "该班级不存在");
        classMapper.deleteById(classId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateClass(ClassUpdateDTO classUpdateDTO) {
        EsClass esClass = classMapper.selectById(classUpdateDTO.getClassId());
        MyAssert.notNull(esClass, "该班级不存在");
        MyAssert.notBlank(classUpdateDTO.getClassName(), "班级名称不能为空");
        esClass.setEsClassName(classUpdateDTO.getClassName());
        classMapper.updateById(esClass);
    }
}




